package en.kaist.cs408.acav;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;









import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import en.kaist.cs408.R;
import en.kaist.cs408.acav.CreateClass.NetworkHandler;

public class StartCheck extends Activity implements OnClickListener{
	
	WifiApManager wifiApManager; 
	TextView texthotspotstat;
	TextView texthotspotspec;
	Button startCheckButton;
	Button hotspotCheckerButton;
	String hotspotAdress;
	String courseID;
	String pwdEncrypt;
	
	public void onCreate(Bundle savedInstanceState) {
	     
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startcheck);
		texthotspotstat = (TextView) findViewById(R.id.hotspot_status);
		texthotspotspec = (TextView) findViewById(R.id.hotspot_spec);
		hotspotCheckerButton = (Button) findViewById(R.id.hotspot_checker);
		startCheckButton = (Button) findViewById(R.id.starter);

		findViewById(R.id.hotspot_checker).setOnClickListener(this);
		findViewById(R.id.starter).setOnClickListener(this);
		
		wifiApManager = new WifiApManager(this);
		if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLED))
		{
			texthotspotstat.setTextColor(Color.GREEN);
			texthotspotstat.setText("ON");
			hotspotAdress = wifiApManager.getMacAdress();
			texthotspotspec.setText("Mac address: " + hotspotAdress);
			startCheckButton.setEnabled(true);
			hotspotCheckerButton.setText("Turn off Wifi Hotspot");
		}
		else
		{
			if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_DISABLED))
			{
				//nothing to do here because it is the default display
			}
		}

	}
	
	 public void onClick(View v) {
			switch (v.getId()) {
			case R.id.hotspot_checker:
				//Log.e(this.getClass().toString(), wifiApManager.getWifiApState().toString());
				//if wifi hotspot is not activated
				if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_DISABLED))
				{

					if(wifiApManager.setWifiApEnabled(null, true))
					{
						Log.e(this.getClass().toString(), "loop disabled:" + wifiApManager.getWifiApState().toString());
						while(!(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLED)))// || wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLING) ))
						{}
							//Log.e(this.getClass().toString(), "in loop enabled:" + wifiApManager.getWifiApState().toString());

							if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLED))//|| wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLING) )
							{
								//Log.e(this.getClass().toString(), "CHANGE TO GREEN");
					            Toast.makeText(getApplicationContext(), "Wifi Hotspot activated", Toast.LENGTH_SHORT).show();
					            texthotspotstat.setTextColor(Color.GREEN);
					            texthotspotstat.setText("ON");
					            hotspotAdress = wifiApManager.getMacAdress();
					            texthotspotspec.setText("Mac address: " + hotspotAdress);
					            startCheckButton.setEnabled(true);
					            hotspotCheckerButton.setText("Turn off Wifi Hotspot");
							}
							else
							{
								if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLING))
								{
									Toast.makeText(getApplicationContext(), "Wifi Hotspot activated", Toast.LENGTH_SHORT).show();
						            texthotspotstat.setTextColor(Color.GREEN);
						            texthotspotstat.setText("ON");
						            hotspotAdress = wifiApManager.getMacAdress();
						            texthotspotspec.setText("Mac address: " + hotspotAdress);
						            startCheckButton.setEnabled(true);
						            hotspotCheckerButton.setText("Turn off Wifi Hotspot");
								}
							}
						
						
						
					}
				}
				else
				{
					//if wifi hotspot is activated
					if(wifiApManager.getWifiApState().equals(WIFI_AP_STATE.WIFI_AP_STATE_ENABLED) && startCheckButton.isEnabled())
					{
						Toast.makeText(getApplicationContext(), "Wifi Hotspot desactivated", Toast.LENGTH_SHORT).show();
			            texthotspotstat.setTextColor(Color.RED);;
			            texthotspotstat.setText("OFF");
			            texthotspotspec.setText("");
			            startCheckButton.setEnabled(false);
			            hotspotCheckerButton.setText("Check WIFI Hotspot");
						if(wifiApManager.setWifiApEnabled(null, false))
						{
							//Deactivating wifi hotspot is quicker than activating it
							//so displayed items are updated first
						}
					}
				}
				break;
			case R.id.starter:
				
				/* Check the input's verification */
				Verifier startCheckVerifier = new Verifier();
				
				EditText cidEditText = (EditText)findViewById(R.id.startcheck_courseID);
				courseID = cidEditText.getText().toString();
				
				EditText pwdEditText = (EditText)findViewById(R.id.startcheck_password);
				String password = pwdEditText.getText().toString();
				
				
				//boolean cid_result = createClassVerifier.verifyCourseID(courseID);
				//int pwd_result = createClassVerifier.verifyPassword(password, password_verif);
				
				if (!startCheckVerifier.verifyCourseID(courseID)){
					Toast.makeText(StartCheck.this, "Course ID is not valid form", Toast.LENGTH_LONG).show();
				}
				else if (password.length()<=7 || password.length()>=21){
					Toast.makeText(StartCheck.this, "Password is not valid form", Toast.LENGTH_LONG).show();
				}
				else{
				
					/* Send to Server (DB update will be occurred in Server-side code)*/
					Toast.makeText(StartCheck.this, "Send Info to server..", Toast.LENGTH_LONG).show();
					
					SHAEncryption shae = new SHAEncryption();
					pwdEncrypt = shae.encryptSHA256(password);
										
					NetworkHandler handler = new NetworkHandler(){
						public void handleMessage(Message msg){
							switch (msg.what){
							case 0:
								Toast.makeText(StartCheck.this, "Server complete", Toast.LENGTH_LONG).show();
								Intent realtimeActivity = new Intent(StartCheck.this, RealTimeAttendanceList.class);
								realtimeActivity.putExtra("CourseID", courseID);
								realtimeActivity.putExtra("PwdEncrypt", pwdEncrypt);
								startActivity(realtimeActivity);
								finish();
								break;
							case -1:
								Toast.makeText(StartCheck.this, "No such course ID", Toast.LENGTH_LONG).show();
								break;
							case -2:
								Toast.makeText(StartCheck.this, "Wrong password", Toast.LENGTH_LONG).show();
								break;
							case -3:
								Toast.makeText(StartCheck.this, "Error in building Recognito", Toast.LENGTH_LONG).show();
							default:
								Toast.makeText(StartCheck.this, "Server got problem...", Toast.LENGTH_LONG).show();
								break;	
							}
						}
							
					};
					/**Thread to startCheking**/
					NetworkThread nt = new NetworkThread(2000, courseID, null, pwdEncrypt, hotspotAdress, null,null,null, false, handler);
					nt.start();
				}
			
					
					
					
				 break;
							
	
			  
			
			}
	 }
	
	
}
